package reservation;

import modelsa.ConnectionRow;
import modelsa.SearchCriteria;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import ui.ReservationMadeListener;

import java.util.ArrayList;
import java.util.List;

public class ReservationMaker {

    private WebDriver driver;

    public ReservationMaker(WebDriver driver) {
        this.driver = driver;
    }

    public void makeReservation(SearchCriteria searchCriteria, ReservationMadeListener reservationMadeListener) {
        System.out.println(searchCriteria);
        driver.get("http://jizdenky.studentagency.cz/");
        LoginProvider.login(driver);

        while (!searchCriteria.getMinutesInDay().isEmpty()) {

            driver.get("http://jizdenky.studentagency.cz/");
            chooseWayAndDate(searchCriteria.getFrom(), searchCriteria.getTo(), searchCriteria.getDate());

            WebUtils.waitForLongAjax();

            List<ConnectionRow> connectionsToClick = new ArrayList<ConnectionRow>();
            List<ConnectionRow> connections = new ArrayList<ConnectionRow>();
            final List<WebElement> elements = driver.findElements(By.cssSelector(".item_blue.blue_gradient"));
            for (WebElement element : elements) {
                final ConnectionRow connectionRow = new ConnectionRow(element);
                connections.add(connectionRow);
                final Integer minuteOfDayArrival = connectionRow.getMinuteOfDayArrival();
                if (searchCriteria.getMinutesInDay().contains(minuteOfDayArrival)) {
                    connectionsToClick.add(connectionRow);
                    searchCriteria.getMinutesInDay().remove(minuteOfDayArrival);
                    reservationMadeListener.reservationMade(minuteOfDayArrival);
                }
            }

            if (connectionsToClick.isEmpty()) {
                WebUtils.waitForNewRequest();
                continue;
            }

            for (ConnectionRow row : connectionsToClick) {
                row.getElement().findElement(By.className("col_price")).click();
            }

            WebUtils.waitForAjax();

            driver.findElement(By.cssSelector("#sumary_lines button")).click();

            driver.findElement(By.cssSelector("input[name='createAndPayTicketButton']")).click();
        }

        driver.quit();
    }

    private void chooseWayAndDate(String from, String to, String date) {
        final WebElement fromCity = driver.findElement(By.cssSelector("#hp_form_z .ui-autocomplete-input"));
        fromCity.clear();
        fromCity.sendKeys(from);
        fromCity.sendKeys(Keys.RETURN);

        final WebElement toCity = driver.findElement(By.cssSelector("#hp_form_do .ui-autocomplete-input"));
        toCity.clear();
        toCity.sendKeys(to);
        toCity.sendKeys(Keys.RETURN);

        final WebElement dateElement = driver.findElement(By.cssSelector("#hp_form_date_to input"));
        dateElement.clear();
        dateElement.sendKeys(date);
        dateElement.sendKeys(Keys.RETURN);
    }

}
